package telegram

import (
	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
	"github.com/spf13/viper"
	"tg-bot/common"
)

var TGBOT *tgbotapi.BotAPI
var GET_TG_KEY = "telegram.bot_api_key"
var GET_TG_DEBUG = "telegram.debug"

func InitTelegramBo() {
	botKey := viper.GetString(GET_TG_KEY)
	if botKey == "" {
		common.SysLog("Not Get Telegram Bot Key, Please Check Config File")
	}
	var err error
	TGBOT, err = tgbotapi.NewBotAPI(botKey)
	if err != nil {
		common.SysLog("Connect Telegram Bot Fail, Please Check Internet and Telegram Bot Key")
		return
	}
	// Enable Start Debug
	var debug bool
	if err := viper.UnmarshalKey(GET_TG_DEBUG, &debug); err != nil {
		debug = true
	}
	TGBOT.Debug = debug
	SetTGBotMenuList()
}

func SendMessageToTGBot(chantId int64, message string) {
	msg := tgbotapi.NewMessage(chantId, message)
	_, err := TGBOT.Send(msg)
	if err != nil {
		common.SysLog("Send Telegram Bot Message Fail, Error ==>>" + err.Error())
	}
}

func ListenTGBOMessage() {
	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60

	updates := TGBOT.GetUpdatesChan(u)
	for update := range updates {
		if update.Message != nil {
			// TG Group add Reply ID
			msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
			switch update.Message.Text {
			case "open":
				msg.Text = "Open Menu List"
				msg.ReplyMarkup = numericKeyboard
			case "line":
				msg.Text = "Choice line Btn"
				msg.ReplyMarkup = inlineKeyBoard
			case "close":
				msg.Text = "Close Menu List"
				msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
			default:
				var requestMessage []common.MessageStruct
				requestMessage = append(requestMessage, common.MessageStruct{
					"user", update.Message.Text,
				})
				result := common.GetAiChatResult(requestMessage)
				msg.ReplyToMessageID = update.Message.MessageID
				msg.Text = result
			}
			TGBOT.Send(msg)
		} else if update.CallbackQuery != nil {
			// Respond to the callback query, telling Telegram to show the user
			// a message with the data received.
			callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data)
			if _, err := TGBOT.Request(callback); err != nil {
				common.SysLog("Telegram Bot Get Callback Fail" + err.Error())
			}
			// And finally, send a message containing the data received.
			msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Data)
			if _, err := TGBOT.Send(msg); err != nil {
				common.SysLog("Telegram Bot CallBack Event Send Message Fail" + err.Error())
			}
		}
	}
}


func SetTGBotMenuList() {
	TGBOT.Request(menuList)
}

func SendGroupMessage(channelName string, sendMsgContent string) {
	msg := tgbotapi.NewMessageToChannel(channelName, sendMsgContent)
	_, err := TGBOT.Send(msg)
	if err != nil {
		common.SysLog("Send Telegram Bot Message Fail, Error ==>>" + err.Error())
	}
}

var numericKeyboard = tgbotapi.NewReplyKeyboard(
	tgbotapi.NewKeyboardButtonRow(
		tgbotapi.NewKeyboardButton("我的"),
		tgbotapi.NewKeyboardButton("订单信息"),
		tgbotapi.NewKeyboardButton("提醒"),
	),
	tgbotapi.NewKeyboardButtonRow(
		tgbotapi.NewKeyboardButton("发现"),
		tgbotapi.NewKeyboardButton("余额"),
		tgbotapi.NewKeyboardButton("帮助"),
	),
)

// 内部按钮
var inlineKeyBoard = tgbotapi.NewInlineKeyboardMarkup(
	tgbotapi.NewInlineKeyboardRow(
		tgbotapi.NewInlineKeyboardButtonURL("官网", "http://subfg.cn"),
		tgbotapi.NewInlineKeyboardButtonData("产品", "2"),
		tgbotapi.NewInlineKeyboardButtonData("套餐", "3"),
	),
	tgbotapi.NewInlineKeyboardRow(
		tgbotapi.NewInlineKeyboardButtonData("我的订阅", "4"),
		tgbotapi.NewInlineKeyboardButtonData("优惠卷", "5"),
		tgbotapi.NewInlineKeyboardButtonData("会员", "6"),
	),
)

// 菜单
var menuList = tgbotapi.NewSetMyCommands(
	tgbotapi.BotCommand{
		"/help", "帮助",
	},
	tgbotapi.BotCommand{
		"/login", "登录",
	},
	tgbotapi.BotCommand{
		"/query", "查询",
	},
)
